package com.lucifer.treasure.network;

import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;

/**
 * 御剑术HUD数据网络包 - 用于客户端和服务端之间的隐蔽数据传输
 */
public class SwordAuraHUDPacket implements CustomPayload {
    
    public static final Identifier PACKET_ID = Identifier.of("lucifer_treasure", "sword_aura_hud");
    public static final Id<SwordAuraHUDPacket> ID = new Id<>(PACKET_ID);
    
    public static final PacketCodec<PacketByteBuf, SwordAuraHUDPacket> CODEC = PacketCodec.of(
        SwordAuraHUDPacket::write,
        SwordAuraHUDPacket::read
    );
    
    private final boolean isEnabled;
    private final int auraLevel;
    private final int visibleSwords;
    private final int hitEffectSwords;
    private final int recoveringSwords;
    private final int maxSwords;
    private final int hostilityCount;
    
    public SwordAuraHUDPacket(boolean isEnabled, int auraLevel, int visibleSwords, 
                             int hitEffectSwords, int recoveringSwords, int maxSwords, int hostilityCount) {
        this.isEnabled = isEnabled;
        this.auraLevel = auraLevel;
        this.visibleSwords = visibleSwords;
        this.hitEffectSwords = hitEffectSwords;
        this.recoveringSwords = recoveringSwords;
        this.maxSwords = maxSwords;
        this.hostilityCount = hostilityCount;
    }
    
    public static void write(SwordAuraHUDPacket packet, PacketByteBuf buf) {
        buf.writeBoolean(packet.isEnabled);
        buf.writeInt(packet.auraLevel);
        buf.writeInt(packet.visibleSwords);
        buf.writeInt(packet.hitEffectSwords);
        buf.writeInt(packet.recoveringSwords);
        buf.writeInt(packet.maxSwords);
        buf.writeInt(packet.hostilityCount);
    }
    
    public static SwordAuraHUDPacket read(PacketByteBuf buf) {
        return new SwordAuraHUDPacket(
            buf.readBoolean(),
            buf.readInt(),
            buf.readInt(),
            buf.readInt(),
            buf.readInt(),
            buf.readInt(),
            buf.readInt()
        );
    }
    
    @Override
    public Id<? extends CustomPayload> getId() {
        return ID;
    }
    
    /**
     * 注册网络包类型
     */
    public static void register() {
        PayloadTypeRegistry.playS2C().register(ID, CODEC);
    }
    
    /**
     * 向客户端发送HUD数据
     */
    public static void sendToClient(ServerPlayerEntity player, boolean isEnabled, int auraLevel, 
                                   int visibleSwords, int hitEffectSwords, int recoveringSwords, 
                                   int maxSwords, int hostilityCount) {
        try {
            SwordAuraHUDPacket packet = new SwordAuraHUDPacket(
                isEnabled, auraLevel, visibleSwords, hitEffectSwords, 
                recoveringSwords, maxSwords, hostilityCount
            );
            ServerPlayNetworking.send(player, packet);
            
        } catch (Exception e) {
            System.err.println("❌ [HUD网络包] 发送失败: " + e.getMessage());
        }
    }
    
    /**
     * 发送错误状态到客户端
     */
    public static void sendErrorToClient(ServerPlayerEntity player) {
        sendToClient(player, false, 0, 0, 0, 0, 0, 0);
    }
    
    // Getters
    public boolean isEnabled() { return isEnabled; }
    public int getAuraLevel() { return auraLevel; }
    public int getVisibleSwords() { return visibleSwords; }
    public int getHitEffectSwords() { return hitEffectSwords; }
    public int getRecoveringSwords() { return recoveringSwords; }
    public int getMaxSwords() { return maxSwords; }
    public int getHostilityCount() { return hostilityCount; }
} 